package com.sicau.smart_farmer.controller;


import com.sicau.smart_farmer.entity.ApplicationMethod;
import com.sicau.smart_farmer.entity.Certificate;
import com.sicau.smart_farmer.entity.UserType;
import com.sicau.smart_farmer.service.ApplicationMethodService;
import com.sicau.smart_farmer.service.CertificateService;
import com.sicau.smart_farmer.service.UserTypeService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/page")
public class PageController {

    @RequestMapping("/{i}")
    public String toPage(@PathVariable("i") String page){
        if (page.contains("\\.")){
            return page.split(".")[0];
        }
        return page;
    }

    @Autowired
    private ApplicationMethodService applicationMethodService;

    @Autowired
    private CertificateService certificateService;

    @Autowired
    private UserTypeService userTypeService;



    @RequestMapping("/{i}/{j}")
    public String toPage1(@PathVariable("i") String flag, @PathVariable("j")String page,
                          @RequestParam(value = "id",required = false) String id,
                          @RequestParam(value = "trainingBatchId",required = false) String trainingBatchId,
                          @RequestParam(value = "needExtralApiInfo",required = false,defaultValue = "false") boolean needExtralApiInfo,
                          @ApiParam(hidden = true) HttpSession session,@ApiParam(hidden = true) HttpServletRequest request){

//        为user信息查看页面传递id参数
        if (id != null){
//            System.out.println(session.getAttribute(id));
            request.setAttribute("checkUser",session.getAttribute(id));
        }

//        为培训批次查看页面传递信息
        if (trainingBatchId != null){
            System.out.println(session.getAttribute(trainingBatchId));
            request.setAttribute("trainingBatch",session.getAttribute(trainingBatchId));
        }





        if(needExtralApiInfo){
            List<ApplicationMethod> allApplicationMethod = applicationMethodService.getAllApplicationMethod();
            List<Certificate> allCertificate = certificateService.getAllCertificate();
            List<UserType> allUserType = userTypeService.getAllUserType();
            request.setAttribute("allApplicationMethod",allApplicationMethod);
            request.setAttribute("allCertificate",allCertificate);
            request.setAttribute("allUserType",allUserType);
        }

        if(page.contains(".")){
            return flag + "/" + page.split("\\.")[0];
        }
        return flag + "/" + page;
    }

}
